<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\pay\controller;

use cmf\controller\AdminBaseController;
use think\db\Query;
use think\facade\Db;

class PayController extends AdminBaseController
{

    /**
     * Notes:充值列表
     * User:
     * DateTime: 2022-01-18 15:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $list = Db::name("member_recharge")->alias('r')
            ->field("r.*,m.user_login")
            ->leftJoin("user m", "r.user_id = m.id")
            ->where(function (Query $query) use ($param) {
                $query->where(['r.delete_time' => 0]);
                if (isset($param['start_time']) && !empty($param['start_time'])) {
                    if (!isset($param['end_time']) || empty($param['end_time'])) {
                        $param['end_time'] = time();
                    }
                    $query->whereTime('r.create_time', 'between', [$param['start_time'], $param['end_time']]);
                }
                if (isset($param['user_login']) && !empty($param['user_login'])) {
                    $query->where('m.user_login', 'like', "%" . $param['user_login'] . "%");
                }
                if (isset($param['status']) && $param['status'] != '') {
                    $query->where(['r.status' => $param['status']]);
                }
                if (isset($param['type']) && $param['type'] != '') {
                    $query->where(['r.type' => $param['type']]);
                }
            })
            ->order("r.id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('user_login', isset($param['user_login']) ? $param['user_login'] : '');
        $this->assign('status', isset($param['status']) ? $param['status'] : '');
        $this->assign('type', isset($param['type']) ? $param['type'] : '');
        return $this->fetch();
    }

    /**
     * Notes:上分页面
     * User:
     * DateTime: 2022-01-19 10:18
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function publish()
    {
        $param = $this->request->param();
        $info = Db::name("member_recharge")->where(['id' => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行上分
     * User:
     * DateTime: 2022-01-19 14:43
     */
    public function doPublish()
    {
        $res = 0;
        $param = $this->request->param();
        try {
            Db::startTrans();
            $member = Db::name("user")->field("balance")->where(["id" => $param['user_id']])->find();
            $data['status'] = 1;
            $data['remark'] = $param['remark'];
            $data['update_time'] = date("Y-m-d H:i:s", time());
            $info = Db::name("member_recharge")->where(['id' => $param['id']])->find();
            $res = Db::name("member_recharge")->where(['id' => $param['id']])->save($data);
            $balance = $info['amount'] + $member['balance'];
            if ($res) Db::name("user")->where(["id" => $param['user_id']])->save(["balance" => $balance]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        if (!$res) $this->error("失败");
        $this->success("成功");
    }

    /**
     * Notes:拒绝页面
     * User:
     * DateTime: 2022-01-19 10:18
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function refuse()
    {
        $param = $this->request->param();
        $info = Db::name("member_recharge")
            ->where(['id' => $param['id']])
            ->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行拒绝
     * User:
     * DateTime: 2022-01-19 14:45
     */
    public function doRefuse()
    {
        $param = $this->request->param();
        $data['status'] = -1;
        $data['remark'] = $param['remark'];
        $data['update_time'] = date("Y-m-d H:i:s", time());
        $res = Db::name("member_recharge")->where(['id' => $param['id']])->save($data);
        if (!$res) $this->error("失败");
        $this->success("成功");
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function delete()
    {
        $this->deleteInfo("member_recharge");
        $this->success("删除成功");
    }

}
